{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import chipwhisperer as cw\n",
    "\n",
    "try:\n",
    "    if not scope.connectStatus:\n",
    "        scope.con()\n",
    "except NameError:\n",
    "    scope = cw.scope()\n",
    "\n",
    "try:\n",
    "    if SS_VER == \"SS_VER_2_1\":\n",
    "        target_type = cw.targets.SimpleSerial2\n",
    "    elif SS_VER == \"SS_VER_2_0\":\n",
    "        raise OSError(\"SS_VER_2_0 is deprecated. Use SS_VER_2_1\")\n",
    "    else:\n",
    "        target_type = cw.targets.SimpleSerial\n",
    "except:\n",
    "    SS_VER=\"SS_VER_1_1\"\n",
    "    target_type = cw.targets.SimpleSerial\n",
    "\n",
    "try:\n",
    "    target = cw.target(scope, target_type)\n",
    "except:\n",
    "    print(\"INFO: Caught exception on reconnecting to target - attempting to reconnect to scope first.\")\n",
    "    print(\"INFO: This is a work-around when USB has died without Python knowing. Ignore errors above this line.\")\n",
    "    scope = cw.scope()\n",
    "    target = cw.target(scope, target_type)\n",
    "\n",
    "\n",
    "print(\"INFO: Found ChipWhisperer😍\")\n",
    "scope.default_setup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# clocks:\n",
    "if PLATFORM == 'CW305_IBEX':\n",
    "    print('Set the CW305 J16 switch to 1 so that Ibex is clocked from HS2. Note this is different from what most other CW305 notebooks require.')\n",
    "\n",
    "scope.clock.clkgen_src = 'system'\n",
    "scope.clock.clkgen_freq = 100e6\n",
    "if scope._is_husky:\n",
    "    scope.clock.adc_mul = 1\n",
    "else:\n",
    "    scope.clock.adc_src = 'clkgen_x1'\n",
    "\n",
    "target.baud = 115200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ibex requires external JTAG programming\n",
    "prog = None\n",
    "\n",
    "# But let's program the FPGA bitfile:\n",
    "if PLATFORM == 'CW305_IBEX':\n",
    "    from chipwhisperer.hardware.firmware.open_fw import getsome_generator\n",
    "    getsome = getsome_generator(\"cw305\")\n",
    "    bsdata = getsome(f\"lowrisc_ibex_demo_system.bit\")\n",
    "    cw305 = cw.target(None, cw.targets.CW305, bsfile=None, force=False)\n",
    "    status = cw305.fpga.FPGAProgram(bsdata, exceptOnDoneFailure=False, prog_speed=10e6)\n",
    "    \n",
    "elif PLATFORM == 'CW312_IBEX':\n",
    "    from chipwhisperer.hardware.firmware.open_fw import getsome_generator\n",
    "    getsome = getsome_generator(\"xc7a35\")\n",
    "    bsdata = getsome(f\"lowrisc_ibex_demo_system.bit\")\n",
    "    #from chipwhisperer.hardware.naeusb.programmer_targetfpga import CW312T_XC7A35T\n",
    "    fpga = cw.hardware.naeusb.programmer_targetfpga.CW312T_XC7A35T(scope)\n",
    "    fpga.program(bsdata, sck_speed=10e6)\n",
    "    status = fpga.done_state()\n",
    "\n",
    "else:\n",
    "    raise ValueError('Unsupported PLATFORM: %s' % PLATFORM)\n",
    "\n",
    "if status:\n",
    "    print(\"✅ FPGA programmed. Next you need to program the firmware using the load_demo_system.sh command from the Ibex repository.\")\n",
    "else:\n",
    "    print(\"❌ FPGA Done pin failed to go high\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reset_target(scope):\n",
    "    raise IOError(\"Default Ibex build does not have Python-driven reset - use the R1 button on the CW305, or the S1 button on the CW313\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
